package com.jinmdz.fmis.mapper.mapper.provider;

import com.jinmdz.fmis.mapper.entity.DataDictionaryEntity;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

/**
 * 数据字典表Provider
 *
 * @author generator
 * @date 2020-03-12 17:08
 */
public class DataDictionaryProvider {

    /**
     * 增加DataDictionary
     *
     * @param entity 数据实体
     * @return String
     * @author generator
     * @date 2020-03-12 17:08
     */
    public String insertDataDictionary(@Param("entity") DataDictionaryEntity entity) {
        return new SQL() {
            {
                INSERT_INTO(" data_dictionary ");

                if (entity.getDeleted() != null) {
                    VALUES(" deleted ", " #{entity.deleted,jdbcType=TINYINT} ");
                }

                VALUES(" dict_code ", " #{entity.dictCode,jdbcType=VARCHAR} ");
                VALUES(" data_code ", " #{entity.dataCode,jdbcType=SMALLINT} ");
                VALUES(" data_text ", " #{entity.dataText,jdbcType=VARCHAR} ");
                VALUES(" data_text_abbr ", " #{entity.dataTextAbbr,jdbcType=VARCHAR} ");
                VALUES(" data_value ", " #{entity.dataValue,jdbcType=DECIMAL} ");
                VALUES(" data_describe ", " #{entity.dataDescribe,jdbcType=VARCHAR} ");

                if (entity.getAsDefault() != null) {
                    VALUES(" as_default ", " #{entity.asDefault,jdbcType=TINYINT} ");
                }


                if (entity.getDisableState() != null) {
                    VALUES(" disable_state ", " #{entity.disableState,jdbcType=TINYINT} ");
                }

                VALUES(" sort ", " #{entity.sort,jdbcType=INTEGER} ");
                VALUES(" remark ", " #{entity.remark} ");
                VALUES(" funeral_parlour_code ", " #{entity.funeralParlourCode,jdbcType=VARCHAR} ");
                VALUES(" create_user_id ", " #{entity.createUserId,jdbcType=INTEGER} ");

                if (entity.getUnmodified() != null) {
                    VALUES(" unmodified ", " #{entity.unmodified,jdbcType=TINYINT} ");
                }

                VALUES(" modified_user_id ", " #{entity.modifiedUserId,jdbcType=INTEGER} ");
                VALUES(" version ", " unix_timestamp(now()) ");
            }
        }.toString();
    }

    /**
     * 修改DataDictionary
     *
     * @param entity 数据实体
     * @return String
     * @author generator
     * @date 2020-03-12 17:08
     */
    public String updateDataDictionary(@Param("entity") DataDictionaryEntity entity) {
        return new SQL() {
            {
                UPDATE(" data_dictionary ");

                if (entity.getDeleted() != null) {
                    SET(" deleted = #{entity.deleted,jdbcType=TINYINT} ");
                }

                SET(" dict_code = #{entity.dictCode,jdbcType=VARCHAR} ");
                SET(" data_code = #{entity.dataCode,jdbcType=SMALLINT} ");
                SET(" data_text = #{entity.dataText,jdbcType=VARCHAR} ");
                SET(" data_text_abbr = #{entity.dataTextAbbr,jdbcType=VARCHAR} ");
                SET(" data_value = #{entity.dataValue,jdbcType=DECIMAL} ");
                SET(" data_describe = #{entity.dataDescribe,jdbcType=VARCHAR} ");

                if (entity.getAsDefault() != null) {
                    SET(" as_default = #{entity.asDefault,jdbcType=TINYINT} ");
                }


                if (entity.getDisableState() != null) {
                    SET(" disable_state = #{entity.disableState,jdbcType=TINYINT} ");
                }

                SET(" sort = #{entity.sort,jdbcType=INTEGER} ");
                SET(" remark = #{entity.remark} ");
                SET(" funeral_parlour_code = #{entity.funeralParlourCode,jdbcType=VARCHAR} ");

                if (entity.getUnmodified() != null) {
                    SET(" unmodified = #{entity.unmodified,jdbcType=TINYINT} ");
                }

                SET(" modified_user_id = #{entity.modifiedUserId,jdbcType=INTEGER} ");
                SET(" version = unix_timestamp(now()) ");
                WHERE(" id = #{entity.id,jdbcType=INTEGER} ");
                WHERE(" unmodified = 0 ");
                WHERE(" version = #{entity.version,jdbcType=INTEGER} ");
            }
        }.toString();
    }

    /**
     * 查询DataDictionary列表
     *
     * @param entity 数据实体
     * @return String
     * @author generator
     * @date 2020-03-12 17:08
     */
    public String listDataDictionary(@Param("entity") DataDictionaryEntity entity) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" id,deleted,dict_code,data_code,data_text,data_text_abbr,data_value,data_describe,as_default,disable_state,sort,remark,funeral_parlour_code,create_user_id,gmt_create,unmodified,modified_user_id,gmt_modified,version ");
                SELECT(builder.toString());
                FROM(" data_dictionary ");

                if (entity.getDictCode() != null) {
                    WHERE(" dict_code = #{entity.dictCode,jdbcType=VARCHAR} ");
                }

                if (entity.getDataCode() != null) {
                    WHERE(" data_code = #{entity.dataCode,jdbcType=SMALLINT} ");
                }

                if (entity.getDataText() != null) {
                    WHERE(" data_text = #{entity.dataText,jdbcType=VARCHAR} ");
                }

                if (entity.getDataTextAbbr() != null) {
                    WHERE(" data_text_abbr = #{entity.dataTextAbbr,jdbcType=VARCHAR} ");
                }

                if (entity.getDataValue() != null) {
                    WHERE(" data_value = #{entity.dataValue,jdbcType=DECIMAL} ");
                }

                if (entity.getDataDescribe() != null) {
                    WHERE(" data_describe = #{entity.dataDescribe,jdbcType=VARCHAR} ");
                }

                if (entity.getAsDefault() != null) {
                    WHERE(" as_default = #{entity.asDefault,jdbcType=TINYINT} ");
                }

                if (entity.getDisableState() != null) {
                    WHERE(" disable_state = #{entity.disableState,jdbcType=TINYINT} ");
                }

                if (entity.getSort() != null) {
                    WHERE(" sort = #{entity.sort,jdbcType=INTEGER} ");
                }

                if (entity.getRemark() != null) {
                    WHERE(" remark = #{entity.remark} ");
                }

                if (entity.getFuneralParlourCode() != null) {
                    WHERE(" funeral_parlour_code = #{entity.funeralParlourCode,jdbcType=VARCHAR} ");
                }

            }
        }.toString();
    }
}